﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using App.MVC.App_Start;
using App.MVC.Extend;

namespace App.MVC
{
    public class BaseController : Controller
    {
        protected BaseController()
        {
            ViewData["Trees"] = SiteInfo.GetTrees();
        }
        /// <summary>
        /// 读取controller及action方便做权限判断
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #region 记录日志
            try
            {


            }
            catch (Exception e)
            {
                LogHelper.WriteLog_Sql(e);
            }

            #endregion
            bool isstate = true;
            isstate = !string.IsNullOrEmpty(SiteInfo.GetUserName());
            if (!isstate) //如果满足
            {

                filterContext.Result = this.RedirectToRoute("Default", new { Controller = "Login", Action = "Index" });
            }
            else
            {
                if (Request.HttpMethod == "GET")
                {
                    //controller
                    var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; //action
                    var actionName = filterContext.ActionDescriptor.ActionName; //获取参数数组
                    var arrParameter = filterContext.ActionDescriptor.GetParameters(); //根据索引获取对应参数名
                    //如果可以确定参数名称可以直接用ActionParameters通过Key来获取，Key指参数名称
                    #region 

                    #endregion
                }
            }


            base.OnActionExecuting(filterContext);

        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="requestContext"></param>
        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
        }
        /// <summary>
        /// 捕捉500错误 404需要配置
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnException(ExceptionContext filterContext)
        {
            #region 记录日志
            try
            {


            }
            catch (Exception e)
            {
                LogHelper.WriteLog_Sql(e);
            }

            #endregion
            // 错误日志编写
            string controllerNamer = filterContext.RouteData.Values["controller"].ToString();
            string actionName = filterContext.RouteData.Values["action"].ToString();
            string exception = filterContext.Exception.ToString();
            // 执行基类中的OnException
            base.OnException(filterContext);
        }

        public JsonResult IsNoAuthority()
        {
            JsonStatus status = new JsonStatus("-1", "无权访问接口，请授权"); var json = new JsonResult(); json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; json.Data = status; return json;
        }

        public class JsonStatus
        {
            public string code { set; get; }
            public string error { set; get; }
            public JsonStatus() { }

            public JsonStatus(string _code, string _error)
            {
                this.code = _code; this.error = _error;
            }
        }

        /// <summary>
        /// 返回JsonResult
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="contentType">内容类型</param>
        /// <param name="contentEncoding">内容编码</param>
        /// <param name="behavior">行为</param>
        /// <returns>JsonReuslt</returns>
        protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new CustomJsonResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior,
                FormateStr = "yyyy-MM-dd"// FormateStr = "yyyy-MM-dd HH:mm:ss"
            };
        }

        /// <summary>
        /// 返回JsonResult.24         /// </summary>
        /// <param name="data">数据</param>
        /// <param name="behavior">行为</param>
        /// <param name="format">json中dateTime类型的格式</param>
        /// <returns>Json</returns>
        protected JsonResult MyJson(object data, JsonRequestBehavior behavior, string format)
        {
            return new CustomJsonResult
            {
                Data = data,
                JsonRequestBehavior = behavior,
                FormateStr = format
            };
        }

        /// <summary>
        /// 返回JsonResult42         
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="format">数据格式</param>
        /// <returns>Json</returns>
        protected JsonResult MyJson(object data, string format)
        {
            return new CustomJsonResult
            {
                Data = data,
                FormateStr = format
            };
        }
    }
}